package com.smartHire.controller.Business;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Goods;
import com.smartHire.service.GoodsService;
import org.bouncycastle.util.Strings;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@WebServlet(name="GoodsAddServlet",value = "/GoodsAddServlet")
public class GoodsAddServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String goods_name=request.getParameter("goods_name");
        String sort = request.getParameter("item_attribute");
        String sale_type = request.getParameter("sale_attribute");
        String repertory_tap = request.getParameter("repertory_tap_first");
        String price = request.getParameter("price");
        String shop_id=request.getParameter("open_id");
        String good_id=request.getParameter("good_id");
        String image=request.getParameter("Img");


        //处理传回的图片链接，获得图片名称
        image = image.replace("[\"", "");
        image = image.replace("\"", "");
        image = image.replace("]", "");
        image = image.replace("http://tmp/", "");
        List <String> Img = Arrays.asList(Strings.split(image,','));


        int Inventory = Integer.parseInt(request.getParameter("total_num"));
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

        //转存照片

        for(int i=0;i<Img.size();i++) {
            String oldPath = "C:/Users/ksevnt/AppData/Local/微信开发者工具/User Data/87e84c5c2e6751b62ab5919b87cb90af/WeappSimulator/WeappFileSystem/o6zAJs76rC2c-qltSl6J1IqpbVt4/wxd272773014bab017/tmp/" + Img.get(i);
            String newPath = "C:/Users/ksevnt/Desktop/智能化共享租赁平台小程序(卖家端)-4-10/img/" + Img.get(i);
            String newPath2 = "C:/Users/ksevnt/Desktop/智能化共享租赁平台小程序-4-13/img/" + Img.get(i);
            p1 t = new p1();
            t.copyFile(oldPath, newPath);
            t.copyFile(oldPath, newPath2);
        }


        //获取service
        try {
            GoodsService goodsServiceservice  = (GoodsService) ctx.getBean("goodsService");

            Goods goods = new Goods();

            goods.setGood_name(goods_name);
            goods.setShop_id(shop_id);
            goods.setInventory(Inventory);


            goods.setPrice(Float.parseFloat(price));
            goods.setSort(sort);

            goods.setRepertory_tap(repertory_tap);
            goods.setSales_tap(sale_type);

            goods.setPicture1("../../img/"+Img.get(0));
            goods.setPicture2("../../img/"+Img.get(1));
            goods.setPicture3("../../img/"+Img.get(2));
            goods.setPicture4("../../img/"+Img.get(3));


        if(good_id.equals("undefined")){
            goods.setAudit("待审核");
            goods.setSalesAmount(0);
            goodsServiceservice.addGoods(goods);
        }

        else{
            goods.setGood_id(Integer.parseInt(good_id));
            goodsServiceservice.updateGoodsById(goods);

        }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

class p1 {
    public void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();

        }

    }

    public void copyFolder(String oldPath, String newPath) {

        try {
            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isFile()) {
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" +
                            (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len;
                    while ((len = input.read(b)) != -1) {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) {//如果是子文件夹
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }
        } catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();

        }

    }
}
